﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace DAO
{
    public class DataAccess
    {
        const string ConnectionString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=QuanLyKho";
        private OleDbConnection _Conntection = new OleDbConnection(ConnectionString);

        public OleDbConnection Conntection
        {
            get { return _Conntection; }
        }

        public DataTable GetData(string SQL, params object[] agms)
        {
            OleDbCommand cmd = new OleDbCommand(SQL, Conntection);
            for (int i = 0; i < agms.Length; i += 3 )
            {
                cmd.Parameters.Add(Utils.ToStr(agms[i + 0]), (OleDbType)agms[i + 2]).Value = agms[i + 1];
            }
            OleDbDataAdapter adap = new OleDbDataAdapter(cmd);
            DataTable tb = new DataTable();
            adap.Fill(tb);
            return tb;
        }
        public object InsertData(string SQL, CommandType cmdType, params object[] agms)
        {
            object obj = null;
            OleDbCommand cmd = new OleDbCommand(SQL, Conntection);
            cmd.CommandType = cmdType;
            for (int i = 0; i < agms.Length; i += 3)
            {
                cmd.Parameters.Add(Utils.ToStr(agms[i + 0]), (OleDbType)agms[i + 2]).Value = agms[i + 1];
            }
            cmd.Connection.Open();
            obj = cmd.ExecuteScalar();
            cmd.Connection.Close();
            return obj;
        }
        public bool UpdateDeleteData(string SQL, CommandType cmdType, params object[] agms)
        {
            int obj = 0;
            OleDbCommand cmd = new OleDbCommand(SQL, Conntection);
            cmd.CommandType = cmdType;
            for (int i = 0; i < agms.Length; i += 3)
            {
                cmd.Parameters.Add(Utils.ToStr(agms[i + 0]), (OleDbType)agms[i + 2]).Value = agms[i + 1];
            }
            cmd.Connection.Open();
            obj = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            return obj > 0;
        }
    }
}
